﻿// -----------------------------------------------------------------------
// <copyright file="NhanVienServices.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Windows.Forms;
using SimpleCrypto;

namespace ASS.BUS.Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ASS.BUS.DTO;
    using ASS.DAO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class NhanVienServices
    {
        public bool Add(NhanVien nhanVien)
        {
            var addSuccsess = false;
            var setSuccess = false;
            using (var db = new DataContext())
            {
                try
                {
                    if (db.NhanViens != null) db.NhanViens.Add(nhanVien);
                    db.SaveChanges();
                    addSuccsess = true;
                }
                catch (Exception ex)
                {
                    addSuccsess = false;
                    MessageBox.Show(ex.Message);
                }
            }

            //if (addSuccsess)
            //{
            //    setSuccess = SetNewPassword(nhanVien.MaNv, nhanVien.Password);
            //}
            return (addSuccsess /*&& setSuccess*/);
        }

        public NhanVien GetNhanVien(int id)
        {
            using (var db = new DataContext())
            {
                var item = db.NhanViens.Single(x => x.MaNv == id);
                return item;
            }
        }

        public List<NhanVien> GetNhanViens()
        {
            using (var db = new DataContext())
            {
                var items = db.NhanViens.ToList();

                return items;
            }
        }

        public List<NhanVien> GetBacSis()
        {
            using (var db = new DataContext())
            {
                var items = db.NhanViens.Where(x => x.LoaiNhanVien == "Bác sĩ").ToList();

                return items;
            }
        }

        public bool SetNewPassword(int id, string newPassword)
        {
            ICryptoService cryptoService = new PBKDF2();
            const int saltSize = 16;
            const int hashIterations = 50;
            using (var db = new DataContext())
            {
                var nhanVien = db.NhanViens.Single(x => x.MaNv == id);
                if (nhanVien != null)
                {
                    //a new password hash is generated from a generated salt with the default settings
                    nhanVien.Password = cryptoService.Compute(newPassword, saltSize, hashIterations);
                    //assigning the generated salt to the user
                    nhanVien.PasswordSalt = cryptoService.Salt;
                    db.SaveChanges();
                    return true;
                }
            }
            return false;
        }

        public bool ValidatePassword(NhanVien user, string password)
        {
            ICryptoService cryptoService = new PBKDF2();
            //hash the password with the saved salt for that user
            string hashed = cryptoService.Compute(password, user.PasswordSalt);
            //return true if both hashes are the same
            return (hashed == user.Password) ? true : false;
        }

        public NhanVien Login(int id, string password)
        {
            using (var db = new DataContext())
            {
                var item = db.NhanViens.Single(x => x.MaNv == id);

                if(item == null)  return null;
                var isValid = ValidatePassword(item, password);

                if (isValid) return item;
            }
            return null;
        }
    }
}
